package models

import (
	"time"

	"github.com/astaxie/beego"
	"github.com/astaxie/beego/logs"
	"github.com/astaxie/beego/orm"
	_ "github.com/go-sql-driver/mysql"
)

type User struct {
	Id         int
	Name       string `orm:"size(20)"`
	Passwd     string
	Department string
	Email      string
	Level      int       `orm:"null,default(0)"`
	CreateTime time.Time `orm:"auto_now_add;type(datetime)"` //创建时间
	UpdateTime time.Time `orm:"auto_now;type(datetime)"`     //修改时间
	Domains    []*Domain `orm:"reverse(many)"`
}

type Domain struct {
	Id      int    `orm:"pk;auto"`
	Title   string `orm:"size(200)"`      //文章标题
	Content string `orm:"size(500)"`      //文章内容
	File    string `orm:"null;size(200)"` //证书路径
	//	Type    string    //文章分类
	CreateTime time.Time `orm:"auto_now_add;type(datetime)"` //创建时间
	UpdateTime time.Time `orm:"auto_now;type(datetime)"`     //修改时间
	DueTime    time.Time `orm:"type(datetime)"`              //到期时间
	Status     bool      `orm:"null,default(false)"`         //状态
	//beego没有使用mysql的原生时间戳，而是自行打时间戳
	DomainType *DomainType `orm:"rel(fk)"`
	User       *User       `orm:"rel(fk)"`
}
type DomainType struct {
	Id       int       `orm:"pk;auto"`
	TypeName string    `orm:"size(20)"`
	User     *User     `orm:"rel(fk)"`
	Domains  []*Domain `orm:"reverse(many)"`
}

type Task struct {
	Id      int    `orm:"pk;auto"`
	Title   string `orm:"size(200)"` //检查标题
	Content string `orm:"size(500)"` //检查周期
	//	Type    string    //分类
	LastTime   time.Time `orm:"type(datetime)"`              //最后执行时间
	CreateTime time.Time `orm:"auto_now_add;type(datetime)"` //创建时间
	UpdateTime time.Time `orm:"auto_now;type(datetime)"`     //修改时间
	Count      int       `orm:"null,default(0)"`             //次数
	//beego没有使用mysql的原生时间戳，而是自行打时间戳
	TaskUser *User `orm:"rel(fk)"` //
}

func AddModel(whichm string, id string) {
	logs.Info("AddMdel for:", whichm, "id is:", id)
}

func DelModel(whichm string, id string) {
	logs.Info("DelModel for:", whichm, "id is:", id)
}

func ShowModelAll(whichm string) {
	logs.Info("ShowModelAll for:", whichm)
}

func ShowModelOne(whichm string) {
	logs.Info("ShowModelOne for:", whichm)
}

// type TaskType struct {
// 	Id       int     `orm:"pk;auto"`
// 	TypeName string  `orm:"size(20)"`
// 	Tasks    []*Task `orm:"reverse(many)"`
// }

func init() {
	dbhost := beego.AppConfig.String("dbhost")
	dbport := beego.AppConfig.String("dbport")
	dbuser := beego.AppConfig.String("dbuser")
	dbpassword := beego.AppConfig.String("dbpassword")
	dbname := beego.AppConfig.String("dbname")
	dball := dbuser + ":" + dbpassword + "@tcp(" + dbhost + ":" + dbport + ")/" + dbname + "?charset=utf8&loc=Asia%2FShanghai"
	orm.RegisterDataBase("default", "mysql", dball)
	// orm.RegisterDataBase("default", "mysql", "root:123456@tcp(127.0.0.1:3306)/CMSdb?charset=utf8&loc=Local")
	orm.RegisterModel(
		new(User),
		new(Domain),
		new(DomainType),
		new(Task),
	// new(TaskType),
	)
	// 只增字段不删
	orm.NewOrm().Update(new(User))
	orm.NewOrm().Update(new(Domain))
	orm.NewOrm().Update(new(DomainType))
	orm.NewOrm().Update(new(Task))

	orm.RunSyncdb("default", false, true)
}
